package com.yonyou.rabbitmq.service.impl;

import com.rabbitmq.client.AMQP;
import com.yonyou.rabbitmq.producer.ReliableRabbitTemplate;
import com.yonyou.rabbitmq.service.BaseProducerService;
import com.yonyou.rabbitmq.service.MessageService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class MessageServiceImpl implements MessageService {

    @Autowired
    BaseProducerService producerService;

    @Override
    public int getMessageCount(String tenantId, String queueName) {
        int queueCount;
        ReliableRabbitTemplate rabbitTemplate = producerService.getTemplateByTenantId(tenantId);
        AMQP.Queue.DeclareOk declareOk = rabbitTemplate.execute(channel -> channel.queueDeclarePassive(queueName));
        queueCount = declareOk.getMessageCount();
        log.info("队列：{},还剩{}条消息",queueName,queueCount);
        return queueCount;
    }
}
